<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
 <title></title>
 <link href="<s:url value='/common/lib/ligerUI/skins/Aqua/css/ligerui-all.css'/>" rel="stylesheet" type="text/css" />
 <link href="<s:url value='/common/lib/ligerUI/skins/ligerui-icons.css'/>" rel="stylesheet" type="text/css" />
 <script src="<s:url value='/common/lib/jquery/jquery-1.3.2.min.js'/>" type="text/javascript"></script>
 <script src="<s:url value='/common/lib/ligerUI/js/core/base.js'/>" type="text/javascript"></script>
 <script src="<s:url value='/common/lib/ligerUI/js/plugins/ligerTab.js'/>" type="text/javascript"></script>
 <script src="<s:url value='/common/lib/ligerUI/js/plugins/ligerDialog.js'/>" type="text/javascript"></script>
 <script src="<s:url value='/common/lib/ajaxfileupload.js'/>" type="text/javascript"></script>
 <script src="<s:url value='/common/dealer.js'/>" type="text/javascript"></script>
 <script type="text/javascript">
 $(function () {
	var dealerCode = $("#dealerCode").val();
	var timeStamp = $("#timeStamp").val();
	if(dealerCode != "" && timeStamp != "") {
		$("#paramBox").hide();
		$("#dc").val(dealerCode);
		$("#ts").val(timeStamp);
		$("#createStatisticFileBut").hide();
		$("#updateStatisticFileBox").hide();
		$("#waitLabel").hide();
		$("#downloadFile").hide();
		$("#recreateStatisticFileBut").hide();
		$("#tabs").hide();
		checkStatisticFileExist(dealerCode, timeStamp)
	} else {
		$("#stroragePathBox").hide();
		$("#createStatisticFileBut").hide();
		$("#updateStatisticFileBox").hide();
		$("#waitLabel").hide();
		$("#downloadFile").hide();
		$("#recreateStatisticFileBut").hide();
		$("#tabs").hide();
		$("#dealerCodeText").focus();
	}
 });
 
 function inputDealerCode() {
	 $.post("<s:url value='/record/validDealerCode.action'/>",{dealerCode : $("#dealerCodeText").val()}, function(data) {
			if(data != 0) {
				$.ligerDialog.alert('您输入的经销商网代码有误，请重新输入!', '提示', 'error');
				$("#dealerCodeText").val("");
				$("#dealerCodeText").focus();
			} else {
				var dc = $("#dealerCodeText").val();
				$.post("<s:url value='/record/getStroragePaths.action'/>",{dealerCode : dc}, function(data) {
	         		var datas = data.split(":");
	         		if(datas[0] == 0) {
	         			$("#dealerCodeText").attr("disabled", "disabled");
	 					$("#dealerCodeInput").attr("disabled", "disabled");
	 					$("#stroragePathBox").show();
	         			$("#stroragePath").prepend("<option value='0'>请选择...</option>");
	         			var dirs = datas[1].split(",");
	         			for(var i = 0; i < dirs.length; i++) {
	         				var dir = dirs[i].replace(/\n/g, "").replace(/\r/g, "");
	         				$("#stroragePath").append("<option value='" + dir + "'>" + dir + "</option>");
	         			}
	         			$("#stroragePath").change(function(){
	     					var val = $("#stroragePath").val();
	     					if(val != 0) {
	     						$("#dc").val(dc);
								$("#ts").val(val);
								checkStatisticFileExist(dc, val);
	     					}
	     				});
	         		} else {
	         			$.ligerDialog.alert('该经销商尚未上传数据!', '提示', 'none'); 
	         		}});
			}
	});
 }
 
 function updateStatisticFile() {
	var fileName = $("#statisticFile").val();
 	var errorMsgTitle = "错误";
	if(fileName == "") {
		$.ligerDialog.alert("请选择统计文件", errorMsgTitle, "error");
		return false;
	}
	$.ligerDialog.confirm('请确定确定统计文件格式是否正确', function (yes) {
		if(yes) {
			var suffix = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();
			if(suffix != "xls") {
				$.ligerDialog.alert("统计文件格式不正确，请上传excel文件", errorMsgTitle, "error");
				return false;
			}
			
			$("#waitLabel").show();
			$("#downloadFile").hide();
			$("#recreateStatisticFileBut").hide();
			$("#updateStatisticFileBox").attr("disabled","disabled");
			
		 	$.ajaxFileUpload ({
	                url:"<s:url value='/record/updateStatisticFile.action'/>",
	                secureuri:false,
	                fileElementId:'statisticFile',
	                dataType: 'text',
	                success: function (data, status) {
	                   if(data == "<pre>1</pre>") {
	                	  $.ligerDialog.alert("统计文件不正确，请上传正确文件", errorMsgTitle, "error");
	                   } else if(data == "<pre>0</pre>") {
	                	  $.ligerDialog.alert("更新成功", "更新成功", "success");
	                   } else {
	                	  $.ligerDialog.alert("系统忙,请稍后再试!", "提示", "warn");
	                   }
	                   $("#waitLabel").hide();
	                   $("#downloadFile").show();
	                   $("#recreateStatisticFileBut").show();
	                   $("#updateStatisticFileBox").attr("disabled","");
	                },
	                error: function (data, status, e) {
	                    alert(e);
	                   $("#waitLabel").hide();
	                   $("#downloadFile").show();
	                   $("#recreateStatisticFileBut").show();
	                  $("#updateStatisticFileBox").attr("disabled","");
	                }
	       	});
			return true;
		}
	});
	return false;
 }
 
 function checkStatisticFileExist(dc, val) {
	 $.post("<s:url value='/record/statisticsFileExist.action'/>",{dealerCode:dc, timeStamp:val}, function(r) {
		if(r == 0) {
			$("#createStatisticFileBut").hide();
			$("#updateStatisticFileBox").show();
			$("#downloadFile").attr("href","<s:url value='/execl-temp/'/>" + dc + "-" + val + ".xls");
 			$("#downloadFile").text("上海大众库存信息整理表" + dc + "-" + val + ".xls");
 			$("#downloadFile").show();
 			$("#recreateStatisticFileBut").show();
 			showData(dc, val);
		} else {
			$("#createStatisticFileBut").show();
			$("#updateStatisticFileBox").hide();
			$("#downloadFile").hide();
			$("#recreateStatisticFileBut").hide();
			showData(dc, val);
		}
	});
 }
 
 function showData(dc, val) {
	$("#tabs").show();
	var tabs = $("#tabs").ligerTab();
	$("#index0").attr("src", "<s:url value='/record/toSatusList.action?index=0'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index1").attr("src", "<s:url value='/record/toSatusList.action?index=1'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index2").attr("src", "<s:url value='/record/toSatusList.action?index=2'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index3").attr("src", "<s:url value='/record/toSatusList.action?index=3'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index4").attr("src", "<s:url value='/record/toSatusList.action?index=4'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index5").attr("src", "<s:url value='/record/toSatusList.action?index=5'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
	$("#index6").attr("src", "<s:url value='/record/toSatusList.action?index=6'/>" + "&timeStamp=" + val + "&dealerCode=" + dc);
 }
 
 function createStatisticFile() {
	 $.ligerDialog.confirm('请确认所有数据都已上传', function (yes) {
 		if(yes) {
			var dc = $("#dc").val();
		 	var ts = $("#ts").val();
		 	$("#waitLabel").show();
		 	$.post("<s:url value='/record/createStatistic.action'/>",{dealerCode:dc,timeStamp:ts}, function(data) {
		 		if(data == 0) {
		 			$("#createStatisticFileBut").hide();
		 			$("#waitLabel").hide();
		 			$("#createStatisticFileBut").attr("disabled","disabled");
		 			$("#downloadFile").attr("href","<s:url value='/execl-temp/'/>" + dc + "-" + ts + ".xls");
		 			$("#downloadFile").text("上海大众库存信息整理表" + dc + "-" + ts + ".xls");
		 			$("#downloadFile").show();
		 			$("#recreateStatisticFileBut").show();
		 			$("#updateStatisticFileBox").show();
		 		} else {
		 			$.ligerDialog.alert('后台忙，生成统计数据失败，请稍候再试!', '提示', 'warn');
		 			$("#createStatisticFileBut").attr("disabled","");
		 			$("#waitLabel").hide();
		 		}
		 	});
 		}
 	});
 }
 
 function recreate() {
	 $.ligerDialog.confirm('确定要重新生成统计表格?', function (yes) {
	 		if(yes) {
				var dc = $("#dc").val();
			 	var ts = $("#ts").val();
			 	$("#downloadFile").hide();
			 	$("#waitLabel").show();
			 	$("#recreateStatisticFileBut").attr("disabled","disabled");
			 	$("#textfield").attr("disabled","disabled");
			 	$("#filebtn").attr("disabled","disabled");
			 	$("#ufilebtn").attr("disabled","disabled");
			 	$.post("<s:url value='/record/createStatistic.action'/>",{dealerCode:dc,timeStamp:ts}, function(data) {
			 		if(data == 0) {
			 			$("#waitLabel").hide();
			 			$("#downloadFile").show();
			 			$("#recreateStatisticFileBut").attr("disabled","");
			 			$("#textfield").attr("disabled","");
					 	$("#filebtn").attr("disabled","");
					 	$("#ufilebtn").attr("disabled","");
			 		} else {
			 			$.ligerDialog.alert('后台忙，生成统计数据失败，请稍候再试!', '提示', 'warn');
			 			$("#waitLabel").hide();
			 			$("#downloadFile").show();
			 			$("#recreateStatisticFileBut").attr("disabled","");
			 			$("#textfield").attr("disabled","");
					 	$("#filebtn").attr("disabled","");
					 	$("#ufilebtn").attr("disabled","");
			 		}
			 	});
	 		}
	 	}); 
 }
 </script>
<style type="text/css">
.file-box {position:relative; width:510px; padding-top: 2px;}
.txt {height:20px; border:1px solid #cdcdcd; width:180px;} 
.file {position:absolute; right:205px; top:0; height:24px; filter:alpha(opacity:0); opacity:0;}
.btn {background-color:#FFF; border:1px solid #CDCDCD; height:24px; width:120px;}
</style>
</head>
<body style="padding:10px">
	<input id="dc" name="dc" type="hidden"/>
	<input id="ts" name="ts" type="hidden"/>
	<s:hidden name="dealerCode"/>
	<s:hidden name="timeStamp"/>
	<div style="width:1690px; height:785px;border:1px solid #A3C0E8;">
		<div id="paramBox" style="margin: 5px;">
			<div id="dealerCodeBox" style="padding:2px;">
				<label style="width: 158px; height: 20px;">请先输入经销商网络代码:</label>
				<input type="text" id="dealerCodeText" name="dealerCodeText" class="l-text" style="width: 120px;  height: 20px;"/>
				<input type="button" id="dealerCodeInput" onclick="inputDealerCode()" value="输入" class="btn"/>
			</div>
			<div id="stroragePathBox" style="padding:2px;">
				<label id="stroragePathLabel" style="width: 158px; height: 20px;">请选择原始数据上传日期:</label>
				<select id="stroragePath" style="width: 120px; height: 20px;"></select>
			</div>
		</div>
		
		<div id="createBox" style="margin: 5px;">
			<input id="createStatisticFileBut" type="button" value="生成统计表格" onclick="createStatisticFile()" class="btn"/>
			<div class="file-box" id="updateStatisticFileBox"> 
				<form id="uploadFileForm" action="#">
					<input type='text' id='textfield' name='textfield' class='txt' /> 
					<input type='button' id="filebtn" class='btn' value='选择统计文件...' /> 
					<input type="file" name="statisticFile" size="28" class="file" id="statisticFile" onchange="document.getElementById('textfield').value=this.value" /> 
					<input type="button" id="ufilebtn" class="btn" value="上传完整信息整理表" onclick="updateStatisticFile()"/> 
				</form> 
			</div> 
			<br/>
			<input id="recreateStatisticFileBut" type="button" value="重新生成统计表格" onclick="recreate()" class="btn"/>
			<label id="waitLabel">正在生成或更新统计数据,请稍候...</label>
			<a id="downloadFile" href="#" target="_blank" title="请点击鼠标右键，选择链接另存为"></a>
		</div>
		
		<div id="tabs" style="width:100%;">
			<div  title="在库车辆-状态一"  style="height:780px" >
				<iframe frameborder="0" id="index0" name="index0" src=""></iframe>
			</div>
			<div title="在库车辆-状态二"   style="height:780px" >
				<iframe frameborder="0" id="index1" name="index1" src=""></iframe>
			</div>
			<div title="在库车辆-状态三"   style="height:780px" >
				<iframe frameborder="0" id="index2" name="index2" src=""></iframe>
			</div>
			<div  title="系统外车辆"  style="height:780px">
				<iframe frameborder="0" id="index3" name="index3" src=""></iframe>
			</div>
			<div  title="在途车辆"   style="height:780px">
				<iframe frameborder="0" id="index4" name="index4" src=""></iframe>
			</div>
			<div  title="未提车辆"  style="height:780px">
				<iframe frameborder="0" id="index5" name="index5" src=""></iframe>
			</div>
			<div  title="虚拟未查到" style="height:780px">
				<iframe frameborder="0" id="index6" name="index6" src=""></iframe>
			</div>
		</div>
	</div>
</body>
</html>